<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>消息频率限制管理 - zut Chat</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #000000 0%, #111111 25%, #1a1a1a 50%, #111111 75%, #000000 100%);
            background-size: 400% 400%;
            animation: darkGradient 20s ease infinite;
            min-height: 100vh;
            padding: 20px;
            color: #333;
        }

        @keyframes darkGradient {
            0%, 100% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
        }

        /* 动态粒子效果 */
        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-image: 
                radial-gradient(1px 1px at 50px 50px, rgba(255, 255, 255, 0.8), transparent),
                radial-gradient(2px 2px at 150px 100px, rgba(255, 255, 255, 0.6), transparent),
                radial-gradient(1px 1px at 250px 150px, rgba(255, 255, 255, 0.4), transparent),
                radial-gradient(2px 2px at 350px 80px, rgba(255, 255, 255, 0.7), transparent);
            background-repeat: repeat;
            background-size: 500px 350px;
            animation: particles 25s linear infinite;
            pointer-events: none;
            z-index: 1;
        }

        @keyframes particles {
            0% { transform: translateY(0px) translateX(0px); opacity: 1; }
            100% { transform: translateY(-50px) translateX(-10px); opacity: 0.3; }
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: linear-gradient(135deg, 
                rgba(255, 255, 255, 0.95) 0%,
                rgba(255, 255, 255, 0.98) 50%,
                rgba(255, 255, 255, 0.95) 100%);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 
                0 20px 60px rgba(0, 0, 0, 0.9),
                0 0 0 1px rgba(255, 255, 255, 0.1),
                inset 0 1px 0 rgba(255, 255, 255, 0.3);
            backdrop-filter: blur(25px);
            position: relative;
            z-index: 10;
            animation: containerGlow 8s ease-in-out infinite;
        }

        @keyframes containerGlow {
            0%, 100% {
                box-shadow: 
                    0 20px 60px rgba(0, 0, 0, 0.9),
                    0 0 0 1px rgba(255, 255, 255, 0.1),
                    inset 0 1px 0 rgba(255, 255, 255, 0.3);
            }
            50% {
                box-shadow: 
                    0 25px 80px rgba(0, 0, 0, 0.95),
                    0 0 0 1px rgba(255, 255, 255, 0.2),
                    inset 0 1px 0 rgba(255, 255, 255, 0.4);
            }
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
            background: linear-gradient(135deg, #07C160 0%, #06A84F 50%, #07C160 100%);
            background-size: 200% 200%;
            animation: headerGlow 6s ease infinite;
            border-radius: 15px;
            color: white;
            box-shadow: 0 8px 32px rgba(7, 193, 96, 0.3);
        }

        @keyframes headerGlow {
            0%, 100% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
        }

        .header h1 {
            font-size: 2.2em;
            margin-bottom: 8px;
            font-weight: 600;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
        }

        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }

        .actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
            flex-wrap: wrap;
            gap: 15px;
            padding: 0 10px;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 25px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            text-align: center;
            position: relative;
            overflow: hidden;
        }

        .btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: left 0.5s;
        }

        .btn:hover::before {
            left: 100%;
        }

        .btn-primary {
            background: linear-gradient(135deg, #07C160 0%, #06A84F 100%);
            color: white;
            box-shadow: 0 4px 15px rgba(7, 193, 96, 0.3);
        }

        .btn-primary:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(7, 193, 96, 0.4);
        }

        .btn-secondary {
            background: linear-gradient(135deg, #6c757d 0%, #5a6268 100%);
            color: white;
            box-shadow: 0 4px 15px rgba(108, 117, 125, 0.3);
        }

        .btn-secondary:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(108, 117, 125, 0.4);
        }

        .btn-success {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
            color: white;
            box-shadow: 0 4px 15px rgba(40, 167, 69, 0.3);
        }

        .btn-success:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(40, 167, 69, 0.4);
        }

        .btn-danger {
            background: linear-gradient(135deg, #dc3545 0%, #e74c3c 100%);
            color: white;
            box-shadow: 0 4px 15px rgba(220, 53, 69, 0.3);
        }

        .btn-danger:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(220, 53, 69, 0.4);
        }

        .btn-warning {
            background: linear-gradient(135deg, #ffc107 0%, #fd7e14 100%);
            color: #333;
            box-shadow: 0 4px 15px rgba(255, 193, 7, 0.3);
        }

        .btn-warning:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(255, 193, 7, 0.4);
        }

        .config-grid {
            display: grid;
            gap: 25px;
            margin-bottom: 30px;
        }

        .config-card {
            background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
            border-radius: 20px;
            padding: 25px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            border: 1px solid rgba(7, 193, 96, 0.1);
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
            position: relative;
            overflow: hidden;
        }

        .config-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(135deg, #07C160 0%, #06A84F 100%);
            transform: scaleX(0);
            transition: transform 0.3s ease;
        }

        .config-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 40px rgba(7, 193, 96, 0.15);
            border-color: rgba(7, 193, 96, 0.3);
        }

        .config-card:hover::before {
            transform: scaleX(1);
        }

        .config-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #f0f0f0;
        }

        .config-title {
            font-size: 1.4em;
            font-weight: 600;
            color: #333;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .user-type-badge {
            padding: 6px 14px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 6px;
            transition: all 0.3s ease;
        }

        .user-type-normal {
            background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
            color: #1976d2;
            border: 1px solid #bbdefb;
        }

        .user-type-admin {
            background: linear-gradient(135deg, #fff3e0 0%, #ffcc02 100%);
            color: #f57c00;
            border: 1px solid #ffcc02;
        }

        .status-toggle {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .status-badge {
            padding: 6px 14px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 6px;
            transition: all 0.3s ease;
        }

        .status-enabled {
            background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%);
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .status-disabled {
            background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%);
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .config-details {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }

        .detail-item {
            background: #f8f9fa;
            padding: 18px;
            border-radius: 12px;
            text-align: center;
            border: 1px solid #e0e0e0;
            transition: all 0.3s ease;
        }

        .detail-item:hover {
            background: rgba(7, 193, 96, 0.05);
            border-color: rgba(7, 193, 96, 0.2);
            transform: translateY(-2px);
        }

        .detail-label {
            font-size: 12px;
            color: #666;
            font-weight: 600;
            margin-bottom: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
        }

        .detail-value {
            font-size: 1.3em;
            color: #333;
            font-weight: 700;
        }

        .config-actions {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            justify-content: center;
        }

        .btn-sm {
            padding: 8px 16px;
            font-size: 12px;
            border-radius: 20px;
            font-weight: 500;
        }

        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.6);
            backdrop-filter: blur(8px);
            animation: modalFadeIn 0.3s ease;
        }

        @keyframes modalFadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        .modal-content {
            background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
            margin: 3% auto;
            padding: 0;
            border-radius: 20px;
            width: 90%;
            max-width: 550px;
            max-height: 90vh;
            overflow: hidden;
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3);
            animation: modalSlideIn 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        }

        @keyframes modalSlideIn {
            from {
                transform: scale(0.8) translateY(-50px);
                opacity: 0;
            }
            to {
                transform: scale(1) translateY(0);
                opacity: 1;
            }
        }

        .modal-header {
            background: linear-gradient(135deg, #07C160 0%, #06A84F 100%);
            color: white;
            padding: 25px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .modal-title {
            font-size: 1.4em;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .close {
            color: rgba(255, 255, 255, 0.8);
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .close:hover {
            color: white;
            background: rgba(255, 255, 255, 0.1);
            transform: rotate(90deg);
        }

        .modal-body {
            padding: 30px;
            max-height: 60vh;
            overflow-y: auto;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
            font-size: 14px;
        }

        .form-control {
            width: 100%;
            padding: 14px 18px;
            border: 2px solid #e0e0e0;
            border-radius: 12px;
            font-size: 14px;
            transition: all 0.3s ease;
            background: white;
        }

        .form-control:focus {
            outline: none;
            border-color: #07C160;
            box-shadow: 0 0 0 3px rgba(7, 193, 96, 0.1);
            transform: translateY(-1px);
        }

        .form-control[readonly] {
            background: #f8f9fa;
            color: #666;
        }

        .form-check {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 18px;
            background: #f8f9fa;
            border-radius: 12px;
            border: 2px solid #e0e0e0;
            transition: all 0.3s ease;
        }

        .form-check:hover {
            border-color: #07C160;
            background: rgba(7, 193, 96, 0.05);
        }

        .form-check input[type="checkbox"] {
            width: 20px;
            height: 20px;
            cursor: pointer;
            accent-color: #07C160;
        }

        .help-text {
            font-size: 12px;
            color: #666;
            margin-top: 8px;
            display: flex;
            align-items: center;
            gap: 6px;
            font-style: italic;
        }

        .form-actions {
            display: flex;
            gap: 15px;
            justify-content: flex-end;
            padding: 20px 30px 30px;
            border-top: 1px solid #f0f0f0;
            background: #f8f9fa;
        }

        .alert {
            padding: 16px 20px;
            margin-bottom: 20px;
            border-radius: 12px;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 10px;
            animation: alertSlideIn 0.3s ease;
        }

        @keyframes alertSlideIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .alert-success {
            background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%);
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%);
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #07C160;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .empty-state {
            text-align: center;
            padding: 60px 40px;
            color: #666;
            background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
            border-radius: 20px;
            border: 2px dashed #e0e0e0;
        }

        .empty-state h3 {
            font-size: 1.5em;
            margin-bottom: 12px;
            color: #333;
        }

        .empty-state p {
            font-size: 1.1em;
            margin-bottom: 25px;
        }

        .empty-state i {
            font-size: 4em;
            color: #07C160;
            margin-bottom: 20px;
            opacity: 0.7;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 20px;
                margin: 10px;
                border-radius: 15px;
            }

            .actions {
                flex-direction: column;
                align-items: stretch;
                gap: 10px;
            }

            .config-details {
                grid-template-columns: 1fr;
            }

            .modal-content {
                margin: 5% auto;
                width: 95%;
                border-radius: 15px;
            }

            .modal-body {
                padding: 20px;
            }

            .form-actions {
                padding: 15px 20px 20px;
                flex-direction: column;
            }

            .config-actions {
                justify-content: center;
            }

            .header h1 {
                font-size: 1.8em;
            }
        }

        /* 滚动条样式 */
        ::-webkit-scrollbar {
            width: 8px;
        }

        ::-webkit-scrollbar-track {
            background: #f1f1f1;
            border-radius: 4px;
        }

        ::-webkit-scrollbar-thumb {
            background: linear-gradient(135deg, #07C160 0%, #06A84F 100%);
            border-radius: 4px;
        }

        ::-webkit-scrollbar-thumb:hover {
            background: linear-gradient(135deg, #06A84F 0%, #059652 100%);
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>
                <i class="fas fa-tachometer-alt"></i>
                消息频率限制管理
            </h1>
            <p>配置不同用户类型的消息发送频率限制，维护聊天室秩序</p>
        </div>

        <div class="actions">
            <button class="btn btn-success" onclick="cleanupExpiredRecords()">
                <i class="fas fa-broom"></i>
                清理过期记录
            </button>
            <div>
                <button class="btn btn-secondary" onclick="refreshConfigs()">
                    <i class="fas fa-sync-alt"></i>
                    刷新配置
                </button>
                <a href="/backend.html" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i>
                    返回后台
                </a>
            </div>
        </div>

        <div id="alertContainer"></div>

        <div id="configGrid" class="config-grid">
            <!-- 配置卡片将在这里动态生成 -->
        </div>
    </div>

    <!-- 编辑配置模态框 -->
    <div id="configModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2 class="modal-title" id="modalTitle">
                    <i class="fas fa-cog"></i>
                    编辑频率限制配置
                </h2>
                <span class="close" onclick="closeConfigModal()">&times;</span>
            </div>
            <div class="modal-body">
                <form id="configForm">
                    <input type="hidden" id="configId">
                    
                    <div class="form-group">
                        <label class="form-label">
                            <i class="fas fa-user-tag"></i>
                            用户类型
                        </label>
                        <input type="text" id="userType" class="form-control" readonly>
                    </div>

                    <div class="form-group">
                        <label class="form-label">
                            <i class="fas fa-clock"></i>
                            每分钟最大消息数
                        </label>
                        <input type="number" id="maxMessagesPerMinute" class="form-control" 
                               min="1" max="1000" required>
                        <div class="help-text">
                            <i class="fas fa-info-circle"></i>
                            用户每分钟最多可以发送的消息数量
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="form-label">
                            <i class="fas fa-hourglass-half"></i>
                            每小时最大消息数
                        </label>
                        <input type="number" id="maxMessagesPerHour" class="form-control" 
                               min="1" max="10000" required>
                        <div class="help-text">
                            <i class="fas fa-info-circle"></i>
                            用户每小时最多可以发送的消息数量
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="form-check">
                            <input type="checkbox" id="enabled" checked>
                            <label class="form-label">
                                <i class="fas fa-power-off"></i>
                                启用频率限制
                            </label>
                        </div>
                        <div class="help-text">
                            <i class="fas fa-info-circle"></i>
                            关闭后此用户类型将不受频率限制
                        </div>
                    </div>
                </form>
            </div>
            <div class="form-actions">
                <button type="button" class="btn btn-secondary" onclick="closeConfigModal()">
                    <i class="fas fa-times"></i>
                    取消
                </button>
                <button type="submit" class="btn btn-primary" form="configForm">
                    <span id="submitText">
                        <i class="fas fa-save"></i>
                        保存配置
                    </span>
                    <span id="submitLoading" class="loading" style="display: none;"></span>
                </button>
            </div>
        </div>
    </div>

    <script>
        let currentConfigs = [];

        // 页面加载时获取配置列表
        document.addEventListener('DOMContentLoaded', function() {
            loadConfigs();
        });

        // 加载频率限制配置
        async function loadConfigs() {
            try {
                const response = await fetch('/api/rate-limit/configs');
                const result = await response.json();

                if (result.success) {
                    currentConfigs = result.data || [];
                    renderConfigs();
                } else {
                    showAlert('获取频率限制配置失败：' + result.message, 'error');
                }
            } catch (error) {
                console.error('加载频率限制配置失败:', error);
                showAlert('加载配置失败：' + error.message, 'error');
            }
        }

        // 渲染配置列表
        function renderConfigs() {
            const configGrid = document.getElementById('configGrid');
            
            if (currentConfigs.length === 0) {
                configGrid.innerHTML = `
                    <div class="empty-state">
                        <i class="fas fa-tachometer-alt"></i>
                        <h3>没有找到频率限制配置</h3>
                        <p>请检查系统配置或联系管理员</p>
                    </div>
                `;
                return;
            }

            configGrid.innerHTML = currentConfigs.map(config => `
                <div class="config-card">
                    <div class="config-header">
                        <div class="config-title">
                            <i class="fas ${config.userType === 'NORMAL' ? 'fa-user' : 'fa-crown'}"></i>
                            ${getUserTypeDisplayName(config.userType)}
                            <span class="user-type-badge ${config.userType === 'NORMAL' ? 'user-type-normal' : 'user-type-admin'}">
                                <i class="fas ${config.userType === 'NORMAL' ? 'fa-user' : 'fa-crown'}"></i>
                                ${config.userType}
                            </span>
                        </div>
                        <div class="status-toggle">
                            <span class="status-badge ${config.enabled ? 'status-enabled' : 'status-disabled'}">
                                <i class="fas ${config.enabled ? 'fa-check-circle' : 'fa-times-circle'}"></i>
                                ${config.enabled ? '已启用' : '已禁用'}
                            </span>
                        </div>
                    </div>
                    
                    <div class="config-details">
                        <div class="detail-item">
                            <div class="detail-label">
                                <i class="fas fa-clock"></i>
                                每分钟限制
                            </div>
                            <div class="detail-value">${config.maxMessagesPerMinute} 条</div>
                        </div>
                        <div class="detail-item">
                            <div class="detail-label">
                                <i class="fas fa-hourglass-half"></i>
                                每小时限制
                            </div>
                            <div class="detail-value">${config.maxMessagesPerHour} 条</div>
                        </div>
                        <div class="detail-item">
                            <div class="detail-label">
                                <i class="fas fa-tachometer-alt"></i>
                                平均频率
                            </div>
                            <div class="detail-value">${(config.maxMessagesPerMinute / 60).toFixed(1)} 条/秒</div>
                        </div>
                        <div class="detail-item">
                            <div class="detail-label">
                                <i class="fas fa-shield-alt"></i>
                                状态
                            </div>
                            <div class="detail-value">${config.enabled ? '限制中' : '无限制'}</div>
                        </div>
                    </div>

                    <div class="config-actions">
                        <button class="btn btn-sm btn-primary" onclick="editConfig(${config.id})" title="编辑配置">
                            <i class="fas fa-edit"></i>
                            编辑配置
                        </button>
                        <button class="btn btn-sm ${config.enabled ? 'btn-danger' : 'btn-success'}" 
                                onclick="toggleConfigEnabled(${config.id}, ${!config.enabled})"
                                title="${config.enabled ? '禁用频率限制' : '启用频率限制'}">
                            <i class="fas ${config.enabled ? 'fa-ban' : 'fa-check'}"></i>
                            ${config.enabled ? '禁用限制' : '启用限制'}
                        </button>
                    </div>
                </div>
            `).join('');
        }

        // 获取用户类型显示名称
        function getUserTypeDisplayName(userType) {
            switch (userType) {
                case 'NORMAL':
                    return '普通用户';
                case 'ADMIN':
                    return '管理员用户';
                default:
                    return userType;
            }
        }

        // 编辑配置
        function editConfig(configId) {
            const config = currentConfigs.find(c => c.id === configId);
            if (!config) return;

            document.getElementById('configId').value = config.id;
            document.getElementById('userType').value = getUserTypeDisplayName(config.userType);
            document.getElementById('maxMessagesPerMinute').value = config.maxMessagesPerMinute;
            document.getElementById('maxMessagesPerHour').value = config.maxMessagesPerHour;
            document.getElementById('enabled').checked = config.enabled;
            document.getElementById('configModal').style.display = 'block';
        }

        // 关闭模态框
        function closeConfigModal() {
            document.getElementById('configModal').style.display = 'none';
        }

        // 处理表单提交
        document.getElementById('configForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const submitText = document.getElementById('submitText');
            const submitLoading = document.getElementById('submitLoading');
            
            submitText.style.display = 'none';
            submitLoading.style.display = 'inline-block';

            try {
                const configId = document.getElementById('configId').value;
                const configData = {
                    maxMessagesPerMinute: parseInt(document.getElementById('maxMessagesPerMinute').value),
                    maxMessagesPerHour: parseInt(document.getElementById('maxMessagesPerHour').value),
                    enabled: document.getElementById('enabled').checked
                };

                const response = await fetch(`/api/rate-limit/config/${configId}`, {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(configData)
                });

                const result = await response.json();

                if (result.success) {
                    showAlert(result.message, 'success');
                    closeConfigModal();
                    loadConfigs();
                } else {
                    showAlert(result.message, 'error');
                }

            } catch (error) {
                console.error('保存配置失败:', error);
                showAlert('保存配置失败：' + error.message, 'error');
            } finally {
                submitText.style.display = 'inline';
                submitLoading.style.display = 'none';
            }
        });

        // 切换配置启用状态
        async function toggleConfigEnabled(configId, enabled) {
            try {
                const config = currentConfigs.find(c => c.id === configId);
                if (!config) return;

                const configData = {
                    ...config,
                    enabled: enabled
                };

                const response = await fetch(`/api/rate-limit/config/${configId}`, {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(configData)
                });

                const result = await response.json();

                if (result.success) {
                    showAlert(enabled ? '频率限制已启用' : '频率限制已禁用', 'success');
                    loadConfigs();
                } else {
                    showAlert(result.message, 'error');
                }
            } catch (error) {
                console.error('切换配置状态失败:', error);
                showAlert('操作失败：' + error.message, 'error');
            }
        }

        // 清理过期记录
        async function cleanupExpiredRecords() {
            if (!confirm('确定要清理过期的用户消息频率记录吗？此操作将删除1天前的记录。')) {
                return;
            }

            try {
                const response = await fetch('/api/rate-limit/cleanup', {
                    method: 'POST'
                });
                const result = await response.json();

                if (result.success) {
                    showAlert('过期记录清理完成', 'success');
                } else {
                    showAlert(result.message, 'error');
                }
            } catch (error) {
                console.error('清理过期记录失败:', error);
                showAlert('清理失败：' + error.message, 'error');
            }
        }

        // 刷新配置列表
        function refreshConfigs() {
            loadConfigs();
        }

        // 显示提示消息
        function showAlert(message, type) {
            const alertContainer = document.getElementById('alertContainer');
            const alertDiv = document.createElement('div');
            alertDiv.className = `alert alert-${type}`;
            
            const icon = type === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle';
            alertDiv.innerHTML = `
                <i class="fas ${icon}"></i>
                ${message}
            `;
            
            alertContainer.appendChild(alertDiv);

            setTimeout(() => {
                alertDiv.style.animation = 'alertSlideIn 0.3s ease reverse';
                setTimeout(() => {
                    alertDiv.remove();
                }, 300);
            }, 5000);
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modal = document.getElementById('configModal');
            if (event.target === modal) {
                closeConfigModal();
            }
        }

        // ESC键关闭模态框
        document.addEventListener('keydown', function(event) {
            if (event.key === 'Escape') {
                closeConfigModal();
            }
        });
    </script>
</body>
</html> 